// pages/user/user-bind/user-bind.js

var httpUtil = require('../../../utils/httpUtil.js')
var emptyUtil = require('../../templates/empty/empty.js')
var uiUtil = require('../../../utils/uiUtil.js')
var util = require('../../../utils/util.js')
const app = getApp()
var that

const REMAIN_SEC = 60; //倒计时总时间60s

Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputPhontNo: null,
    inputSmsCode: null,

    sendSmsCodeStatus: 0, //0:默认,1:请稍候,2:倒计时
    remainSec: 60, //获取验证码倒计时
    timer: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
  },
  onUnload: function() {
    var timer = this.data.timer;
    if (null != timer) {
      clearInterval(timer);
    }
  }, 
  onShareAppMessage: function (res) {
    return util.handleShareApp()
  },
  /**
   * 手机号输入
   */
  onPhoneNoInput(e) {
    var value = e.detail.value
    this.setData({
      inputPhontNo: value
    })
  },
  /**
   * 短信验证码输入
   */
  onSmsCodeInput(e) {
    var value = e.detail.value
    this.setData({
      inputSmsCode: value
    })
  },
  /**
   * 获取手机验证码点击
   */
  requestGetSmsCode() {
    if (this.data.sendSmsCodeStatus != 0) {
      return;
    }

    var phoneNo = this.data.inputPhontNo
    if (!phoneNo) {
      uiUtil.showToast("请输入手机号码")
      return
    }

    //切换界面显示
    this.setData({
      sendSmsCodeStatus: 1,
    })

    //发送验证码
    httpUtil.PostRequest("send_sms_code", {
      phoneNo: phoneNo,
      codeType: 'userBind',
    }, {
      success(res) {
        //切换界面显示
        that.setData({
          sendSmsCodeStatus: 2,
        })

        //开启倒计时
        var remain = that.data.remainSec
        var time = setInterval(function() {
          if (remain == 1) {
            clearInterval(time);
            that.setData({
              remainSec: REMAIN_SEC,
              sendSmsCodeStatus: 0,
            })
            return false
          }
          remain--;
          that.setData({
            remainSec: remain
          })
        }, 1000)
        that.setData({
          timer: time
        })
      },
      fail(code, res) {
        //切换界面显示
        that.setData({
          sendSmsCodeStatus: 0,
        })

        var text = emptyUtil.getErrText(code, res, "验证码发送失败，请稍候重试")
        uiUtil.showToast(text)
      },
      complete() {}
    })


  },

  /**
   * 激活
   */
  requestActivate() {
    var phoneNo = this.data.inputPhontNo
    if (!phoneNo) {
      uiUtil.showToast("请输入手机号码")
      return
    }

    var smsCode = this.data.inputSmsCode
    if (!smsCode) {
      uiUtil.showToast("请输入验证码")
      return
    }

    uiUtil.showLoading("请稍候..")
    wx.login({
      success: res => {
        httpUtil.PostRequest("vip_bind", {
          phoneNo: phoneNo,
          smsCode: smsCode,
          wxLoginCode: res.code
        }, {
          success(res) {
            uiUtil.hideLoading()
            app.globalData.loginStatus = 2
            app.globalData.loginCode = res.loginId
            app.globalData.identity = res.identity
            wx.switchTab({
              url: '/pages/index/home/home',
            })
          },
          fail(code, res) {
            var text = emptyUtil.getErrText(code, res, "激活失败,若有疑问请联系客服")
            uiUtil.showToast(text)
          },
          complete() {}
        })
      }
    })

  },

})
